const api = require('../../../api/index.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imgSrc: api.imgPathUrl
    })
    let kid = options.kid
    let vid = options.vid
    let discount_id = options.discount_id
    let moblic = options.mobmic
    let num = options.num;
    this.setData({
      kid: kid,
      vid: vid,
      discount_id: discount_id,
      moblic: moblic,
      num: num
    })
    let data = {
      Discount_id : discount_id,
      Kid : kid
    }
    this.CouponList(data)
  },
  CouponList: function (data){
    let thit = this 
    api.PayCoupon(data,(res)=>{
      console.log(res);
      this.setData({
        Coupon:res.data.result
      })
      if (res.data.result.length === 0) {
        thit.setData({
          without: true
        })
      } else {
        thit.setData({
          without: false
        })
      }
    })
  },
  noPayCoupon:function(){  //不选择优惠券
    let thit = this;
    this.data.Coupon.forEach((item,index)=>{
      let key = `Coupon[${index}].choice`
      this.setData({
        [key]:0
      })
    })
    this.setData({
      discount_id:''
    })
    let k_id = this.data.kid
    let v_id = this.data.vid
    let phone = this.data.moblic
    let num = this.data.num
    wx.setStorageSync('onloDanD', '1')

    wx.setStorageSync('payOp', {
      "k_id": k_id,
      "v_id": v_id,
      "mobile": phone,
      "num": num,
      "type":3
    });


    wx.removeStorageSync('discount_id')

    wx.navigateBack({
      delta: 1,
    })
    // wx.redirectTo({
    //   url: `/pages/my/play/play?k_id=${k_id}&v_id=${v_id}&mobile=${phone}&type=${3}&num=${num}`,
    // })
  },
  selet:function(e){  //选择优惠券
      let index = e.currentTarget.dataset.index;
      let id = e.currentTarget.dataset.id;
      let thit = this;
      this.data.Coupon.forEach((item,index)=>{
        let key = `Coupon[${index}].choice`
        if (item.is_use != '' ){
          thit.setData({
            [key]:0
          })
        }
      })
      if (this.data.Coupon.is_use != '') {
        thit.setData({
          [`Coupon[${index}].choice`]: 1,
          discount_id:id
        })
      }
      let k_id = this.data.kid
      let v_id = this.data.vid
      let phone = this.data.moblic
      let num = this.data.num
      wx.setStorageSync('onloDanD', '1')
      wx.setStorageSync('payOp', {
        "k_id": k_id,
        "v_id": v_id,
        "mobile": phone,
        "num": num,
        "discount_id":id
      });
      wx.navigateBack({
        delta: 1,
      })
      
      
      // wx.redirectTo({ 
      //   url: `/pages/my/play/play?k_id=${k_id}&v_id=${v_id}&mobile=${phone}&discount_id=${id}&num=${num}`,
      // })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },


})
